package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysWxUser;
import com.ruoyi.system.domain.vo.SaveWxUserVo;
import com.ruoyi.system.service.IJjzApplyService;
import com.ruoyi.system.service.ISysWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/wx/user")
public class SysWxUserController extends BaseController
{
    @Autowired
    private ISysWxUserService wxUserService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IJjzApplyService jjzApplyService;
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysWxUser sysWxUser)
    {
        startPage();
        List<SysWxUser> list = wxUserService.selectWxUserList(sysWxUser);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysWxUser sysWxUser)
    {
        List<SysWxUser> list = wxUserService.selectWxUserList(sysWxUser);
        ExcelUtil<SysWxUser> util = new ExcelUtil<SysWxUser>(SysWxUser.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wxUserService.selectWxUserById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysWxUser sysWxUser)
    {
        return toAjax(wxUserService.insertWxUser(sysWxUser));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysWxUser sysWxUser)
    {
        return toAjax(wxUserService.updateWxUser(sysWxUser));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wxUserService.deleteWxUserByIds(ids));
    }


   // @Log(title = "保存数据", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult saveData(@RequestBody SaveWxUserVo vo)
    {
        wxUserService.saveData(vo);
        return AjaxResult.success();
    }

   // @Log(title = "获取用户详情", businessType = BusinessType.INSERT)
    @GetMapping("/info")
    public AjaxResult getWxInfo(String openid)
    {
        SaveWxUserVo sysWxUser = wxUserService.selectWxUserByOpenid(openid);
        return AjaxResult.success(sysWxUser);
    }

    @Log(title = "初始化数据", businessType = BusinessType.INSERT)
    @PutMapping("/init")
    public AjaxResult initData(@RequestParam("auth")String auth,  @RequestParam("openid") String openid, @RequestParam("expTime") String expTime, @RequestParam("carInfoId")Long carInfoId)
    {
        wxUserService.initData(auth,openid,expTime,carInfoId);
        return AjaxResult.success();
    }

    @Log(title = "更新状态", businessType = BusinessType.INSERT)
    @GetMapping("/checkApply")
    public AjaxResult checkApply(@RequestParam("auth")String auth)
    {
        jjzApplyService.checkApplyJjz(auth);
        return AjaxResult.success();
    }


    @GetMapping("/txt")
    public AjaxResult txt()
    {
        String str = wxUserService.txt();
        return AjaxResult.success("成功",str);
    }
    @GetMapping("/show")
    public AjaxResult show()
    {
        Integer str = wxUserService.show();
        return AjaxResult.success("成功",str);
    }


    @Log(title = "获取用户详情", businessType = BusinessType.INSERT)
    @GetMapping("/test")
    public AjaxResult test()
    {
        // 存入reds 设置过期时间
        redisCache.setCacheObject("key","11111111",2, TimeUnit.MINUTES);
        jjzApplyService.checkApplyJjz("e1d33ff35d0248f587c1da7da126db8e");
        return AjaxResult.success();
    }


}
